import request from '../../utils/request'
Page({
  data: {
    phone: '',
    password: ''
  },
  onLoad(options) {

  },
  handleInput(e) {
    let type = e.currentTarget.id
    this.setData({
      [type]: e.detail.value
    })
  },

  async login() {
    const {
      phone,
      password
    } = this.data
    if (!phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
      return
    }
    let phoneReg = /^1[3|4|5|6|7|8|9][0-9]{9}$/
    if (!phoneReg.test(phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return
    }
    if (!password) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '登录中',
    })
    let res = await request("login/cellphone", {
      phone,
      password,
      isLogin: true
    })
    if (res.code === 200) {
      wx.setStorageSync('userInfo', JSON.stringify(res.profile))
      wx.reLaunch({
        url: '/pages/personal/personal',
      })
      // wx.navigateBack({
      //   delta: -1,
      // })
      wx.hideLoading()
      wx.showToast({
        title: '登录成功',
      })
    } else {
      return wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    }
  },
  onShareAppMessage() {},
  onShareTimeline() {}

})